// BroadcastSender.c 

 

#include <stdio.h> /* for printf() and fprintf() */ 

#include <sys/socket.h> /* for socket() and bind() */ 

#include <arpa/inet.h> /* for sockaddr_in */ 

#include <stdlib.h> /* for atoi() and exit() */ 

#include <string.h> /* for memset() */ 

#include <unistd.h> /* for close() */ 

 

void DieWithError(char *errorMessage); /* External error handling function */ 

 

int main(int argc, char *argv[]) 

{ 

 int sock; /* Socket */ 

 struct sockaddr_in broadcastAddr; /* Broadcast address */ 

 char *broadcastIP; /* IP broadcast address */ 

 unsigned short broadcastPort; /* Server port */ 

 char *sendString; /* String to broadcast */ 

 int broadcastPermission; /* Socket opt to set permission to broadcast */ 

 unsigned int sendStringLen; /* Length of string to broadcast */ 

 

 if (argc < 4) /* Test for correct number of parameters */ 

 { 

 fprintf(stderr,"Usage: %s <IP Address> <Port> <Send String>\n", argv[0]); 

 exit(1); 

 } 

 

 broadcastIP = argv[1]; /* First arg: broadcast IP address */ 

 broadcastPort = atoi(argv[2]); /* Second arg: broadcast port */ 

 sendString = argv[3]; /* Third arg: string to broadcast */ 

 


/* Create socket for sending/receiving datagrams */ 

 if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) 

 DieWithError("socket() failed"); 

 

 /* Set socket to allow broadcast */ 

 broadcastPermission = 1; 

 if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void *) &broadcastPermission, 

 sizeof(broadcastPermission)) < 0) 

 DieWithError("setsockopt() failed"); 

 

 /* Construct local address structure */ 

 memset(&broadcastAddr, 0, sizeof(broadcastAddr)); /* Zero out structure */ 

 broadcastAddr.sin_family = AF_INET; /* Internet address family */ 

 broadcastAddr.sin_addr.s_addr = inet_addr(broadcastIP);/* Broadcast IP address */ 

 broadcastAddr.sin_port = htons(broadcastPort); /* Broadcast port */ 

 

 sendStringLen = strlen(sendString); /* Find length of sendString */ 

 for (;;) /* Run forever */ 

 { 

 /* Broadcast sendString in datagram to clients every 3 seconds*/ 

 if (sendto(sock, sendString, sendStringLen, 0, (struct sockaddr *) 

 &broadcastAddr, sizeof(broadcastAddr)) != sendStringLen) 

 DieWithError("sendto() sent a different number of bytes than expected"); 

 

 sleep(3); /* Avoids flooding the network */ 

 } 

 /* NOT REACHED */ 

} 

 

 

 


// BroadcaseReceiver.c 

 

#include <stdio.h> /* for printf() and fprintf() */ 

#include <sys/socket.h> /* for socket(), connect(), sendto(), and recvfrom() */ 

#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */ 

#include <stdlib.h> /* for atoi() and exit() */ 

#include <string.h> /* for memset() */ 

#include <unistd.h> /* for close() */ 

 

#define MAXRECVSTRING 255 /* Longest string to receive */ 

 

void DieWithError(char *errorMessage); /* External error handling function */ 

 

int main(int argc, char *argv[]) 

{ 

 int sock; /* Socket */ 

 struct sockaddr_in broadcastAddr; /* Broadcast Address */ 

 unsigned short broadcastPort; /* Port */ 

 char recvString[MAXRECVSTRING+1]; /* Buffer for received string */ 

 int recvStringLen; /* Length of received string */ 

 

 if (argc != 2) /* Test for correct number of arguments */ 

 { 

 fprintf(stderr,"Usage: %s <Broadcast Port>\n", argv[0]); 

 exit(1); 

 } 

 

 broadcastPort = atoi(argv[1]); /* First arg: broadcast port */ 

 

 /* Create a best-effort datagram socket using UDP */ 

 if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) 


DieWithError("socket() failed"); 

 

 /* Construct bind structure */ 

 memset(&broadcastAddr, 0, sizeof(broadcastAddr)); /* Zero out structure */ 

 broadcastAddr.sin_family = AF_INET; /* Internet address family */ 

 broadcastAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */ 

 broadcastAddr.sin_port = htons(broadcastPort); /* Broadcast port */ 

 

 /* Bind to the broadcast port */ 

 if (bind(sock, (struct sockaddr *) &broadcastAddr, sizeof(broadcastAddr)) < 0) 

 DieWithError("bind() failed"); 

 

 /* Receive a single datagram from the server */ 

 if ((recvStringLen = recvfrom(sock, recvString, MAXRECVSTRING, 0, NULL, 0)) < 0) 

 DieWithError("recvfrom() failed"); 

 

 recvString[recvStringLen] = '\0'; 

 printf("Received: %s\n", recvString); /* Print the received string */ 

 

 close(sock); 

 exit(0); 

} 

 

 

 

 

 

 

 

 

 


// MulticastSender.c 

 

#include <stdio.h> /* for fprintf() */ 

#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */ 

#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */ 

#include <stdlib.h> /* for atoi() and exit() */ 

#include <string.h> /* for memset() */ 

#include <unistd.h> /* for sleep() */ 

 

void DieWithError(char *errorMessage); /* External error handling function */ 

 

int main(int argc, char *argv[]) 

{ 

 int sock; /* Socket */ 

 struct sockaddr_in multicastAddr; /* Multicast address */ 

 char *multicastIP; /* IP Multicast address */ 

 unsigned short multicastPort; /* Server port */ 

 char *sendString; /* String to multicast */ 

 unsigned char multicastTTL; /* TTL of multicast packets */ 

 unsigned int sendStringLen; /* Length of string to multicast */ 

 

 if ((argc < 4) || (argc > 5)) /* Test for correct number of parameters */ 

 { 

 fprintf(stderr,"Usage: %s <Multicast Address> <Port> <Send String> [<TTL>]\n", 

 argv[0]); 

 exit(1); 

 } 

 

 multicastIP = argv[1]; /* First arg: multicast IP address */ 

 multicastPort = atoi(argv[2]); /* Second arg: multicast port */ 

 sendString = argv[3]; /* Third arg: String to multicast */ 


if (argc == 5) /* Is TTL specified on command-line? */ 

 multicastTTL = atoi(argv[4]); /* Command-line specified TTL */ 

 else 

 multicastTTL = 1; /* Default TTL = 1 */ 

 

 /* Create socket for sending/receiving datagrams */ 

 if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) 

 DieWithError("socket() failed"); 

 

 /* Set TTL of multicast packet */ 

 if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, (void *) &multicastTTL, 

 sizeof(multicastTTL)) < 0) 

 DieWithError("setsockopt() failed"); 

 

 /* Construct local address structure */ 

 memset(&multicastAddr, 0, sizeof(multicastAddr)); /* Zero out structure */ 

 multicastAddr.sin_family = AF_INET; /* Internet address family */ 

 multicastAddr.sin_addr.s_addr = inet_addr(multicastIP);/* Multicast IP address */ 

 multicastAddr.sin_port = htons(multicastPort); /* Multicast port */ 

 

 sendStringLen = strlen(sendString); /* Find length of sendString */ 

 for (;;) /* Run forever */ 

 { 

 /* Multicast sendString in datagram to clients every 3 seconds */ 

 if (sendto(sock, sendString, sendStringLen, 0, (struct sockaddr *) 

 &multicastAddr, sizeof(multicastAddr)) != sendStringLen) 

 DieWithError("sendto() sent a different number of bytes than expected"); 

 sleep(3); 

 } 

 /* NOT REACHED */ 

} 


// MulticastReceiver.c 

 

#include <stdio.h> /* for printf() and fprintf() */ 

#include <sys/socket.h> /* for socket(), connect(), sendto(), and recvfrom() */ 

#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */ 

#include <stdlib.h> /* for atoi() and exit() */ 

#include <string.h> /* for memset() */ 

#include <unistd.h> /* for close() */ 

 

#define MAXRECVSTRING 255 /* Longest string to receive */ 

 

void DieWithError(char *errorMessage); /* External error handling function */ 

 

int main(int argc, char *argv[]) 

{ 

 int sock; /* Socket */ 

 struct sockaddr_in multicastAddr; /* Multicast Address */ 

 char *multicastIP; /* IP Multicast Address */ 

 unsigned short multicastPort; /* Port */ 

 char recvString[MAXRECVSTRING+1]; /* Buffer for received string */ 

 int recvStringLen; /* Length of received string */ 

 struct ip_mreq multicastRequest; /* Multicast address join structure */ 

 

 if (argc != 3) /* Test for correct number of arguments */ 

 { 

 fprintf(stderr,"Usage: %s <Multicast IP> <Multicast Port>\n", argv[0]); 

 exit(1); 

 } 

 

 multicastIP = argv[1]; /* First arg: Multicast IP address (dotted quad) */ 

 multicastPort = atoi(argv[2]);/* Second arg: Multicast port */ 


/* Create a best-effort datagram socket using UDP */ 

 if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) 

 DieWithError("socket() failed"); 

 

 /* Construct bind structure */ 

 memset(&multicastAddr, 0, sizeof(multicastAddr)); /* Zero out structure */ 

 multicastAddr.sin_family = AF_INET; /* Internet address family */ 

 multicastAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */ 

 multicastAddr.sin_port = htons(multicastPort); /* Multicast port */ 

 

 /* Bind to the multicast port */ 

 if (bind(sock, (struct sockaddr *) &multicastAddr, sizeof(multicastAddr)) < 0) 

 DieWithError("bind() failed"); 

 

 /* Specify the multicast group */ 

 multicastRequest.imr_multiaddr.s_addr = inet_addr(multicastIP); 

 /* Accept multicast from any interface */ 

 multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY); 

 /* Join the multicast address */ 

 if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *) &multicastRequest, 

 sizeof(multicastRequest)) < 0) 

 DieWithError("setsockopt() failed"); 

 

 /* Receive a single datagram from the server */ 

 if ((recvStringLen = recvfrom(sock, recvString, MAXRECVSTRING, 0, NULL, 0)) < 0) 

 DieWithError("recvfrom() failed"); 

 

 recvString[recvStringLen] = '\0'; 

 printf("Received: %s\n", recvString); /* Print the received string */ 

 

 close(sock); 


exit(0); 

} 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


// TCPEchoServer-Fork.c 

 

#include "TCPEchoServer.h" /* TCP echo server includes */ 

#include <sys/wait.h> /* for waitpid() */ 

 

int main(int argc, char *argv[]) 

{ 

 int servSock; /* Socket descriptor for server */ 

 int clntSock; /* Socket descriptor for client */ 

 unsigned short echoServPort; /* Server port */ 

 pid_t processID; /* Process ID from fork() */ 

 unsigned int childProcCount = 0; /* Number of child processes */ 

 

 if (argc != 2) /* Test for correct number of arguments */ 

 { 

 fprintf(stderr, "Usage: %s <Server Port>\n", argv[0]); 

 exit(1); 

 } 

 

 echoServPort = atoi(argv[1]); /* First arg: local port */ 

 

 servSock = CreateTCPServerSocket(echoServPort); 

 

 for (;;) /* Run forever */ 

 { 

 clntSock = AcceptTCPConnection(servSock); 

 /* Fork child process and report any errors */ 

 if ((processID = fork()) < 0) 

 DieWithError("fork() failed"); 

 else if (processID == 0) /* If this is the child process */ 

 { 


close(servSock); /* Child closes parent socket */ 

 HandleTCPClient(clntSock); 

 

 exit(0); /* Child process terminates */ 

 } 

 

 printf("with child process: %d\n", (int) processID); 

 close(clntSock); /* Parent closes child socket descriptor */ 

 childProcCount++; /* Increment number of outstanding child processes */ 

 

 while (childProcCount) /* Clean up all zombies */ 

 { 

 processID = waitpid((pid_t) -1, NULL, WNOHANG); /* Non-blocking wait */ 

 if (processID < 0) /* waitpid() error? */ 

 DieWithError("waitpid() failed"); 

 else if (processID == 0) /* No zombie to wait on */ 

 break; 

 else 

 childProcCount--; /* Cleaned up after a child */ 

 } 

 } 

 /* NOT REACHED */ 

} 

 

 

 

 

 

 

 

 


// TCPEchoServer-ForkN.c 

 

#include "TCPEchoServer.h" 

 

void ProcessMain(int servSock); /* Main program of process */ 

 

int main(int argc, char *argv[]) 

{ 

 int servSock; /* Socket descriptor for server*/ 

 unsigned short echoServPort; /* Server port */ 

 pid_t processID; /* Process ID */ 

 unsigned int processLimit; /* Number of child processes to create */ 

 unsigned int processCt; /* Process counter */ 

 

 if (argc != 3) /* Test for correct number of arguments */ 

 { 

 fprintf(stderr,"Usage: %s <SERVER PORT> <FORK LIMIT>\n", argv[0]); 

 exit(1); 

 } 

 

 echoServPort = atoi(argv[1]); /* First arg: local port */ 

 processLimit = atoi(argv[2]); /* Second arg: number of child processes */ 

 

 servSock = CreateTCPServerSocket(echoServPort); 

 

 for (processCt=0; processCt < processLimit; processCt++) 

 /* Fork child process and report any errors */ 

 if ((processID = fork()) < 0) 

 DieWithError("fork() failed"); 

 else if (processID == 0) /* If this is the child process */ 

 ProcessMain(servSock); 




 exit(0); /* The children will carry on */ 

} 

 

void ProcessMain(int servSock) 

{ 

 int clntSock; /* Socket descriptor for client connection */ 

 

 for (;;) /* Run forever */ 

 { 

 clntSock = AcceptTCPConnection(servSock); 

 printf("with child process: %d\n", (unsigned int) getpid()); 

 HandleTCPClient(clntSock); 

 } 

} 

 


